<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card>
      <!-- 警告 -->
      <el-alert
        :closable="false"
        class="el-icon-info"
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
      >
      </el-alert>
      <!-- 下拉菜单 -->
      <el-row>
        <el-col :span="20">
          <span>选择商品分类： </span>
          <!-- 级联下拉单 -->
          <el-cascader
            style="width: 250px"
            v-model="selectedKeys"
            :options="cateList"
            @change="handleChange(selectedKeys)"
            :props="cateProps"
          ></el-cascader>
        </el-col>
      </el-row>
      <!-- tabs -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            size="mini"
            type="primary"
            :disabled="isdisabled"
            @click="addAttr('query')"
            >添加参数</el-button
          >
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column width="60" type="expand"> </el-table-column>
            <el-table-column width="60" type="index"> #</el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <div class="btns">
                  <el-button
                    icon="el-icon-edit"
                    size="mini"
                    type="primary"
                    @click="openDialog('edit', scope.row.id)"
                  ></el-button>
                  <el-button
                    icon="el-icon-delete"
                    size="mini"
                    type="danger"
                    @click="openDialog('del', scope.row.id)"
                  ></el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button
            size="mini"
            type="primary"
            :disabled="isdisabled"
            @click="addAttr('attr')"
            >添加属性</el-button
          >
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column width="60" type="expand"> </el-table-column>
            <el-table-column width="60" type="index"># </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <div class="btns">
                  <el-button
                    icon="el-icon-edit"
                    size="mini"
                    type="primary"
                    @click="openDialog('edit', scope.row.id)"
                  ></el-button>
                  <el-button
                    icon="el-icon-delete"
                    size="mini"
                    type="danger"
                    @click="openDialog('del', scope.row.id)"
                  ></el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 公共对话框 -->
    <el-dialog
      :title="'添加' + titleTop"
      :visible.sync="dialogVisible"
      width="30%"
      @close="addDialogclose"
    >
      <el-form :model="addForm" ref="addFormRef" label-width="80px">
        <el-form-item
          :label="titleTop"
          prop="attr_name"
          :rules="{ required: true, message: '输入不能为空', trigger: 'blur' }"
        >
          <el-input v-model="addForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { querygoodsAPI, goodsortAPI, choicequeryAPI, addqueryAPI } from '@/api/goods.js'
export default {
  name: '',
  data () {
    return {
      addForm: {
        attr_name: ''
      },
      titleTop: '',
      //对话框默认关闭
      dialogVisible: false,
      //  tabs 
      activeName: 'many',
      // 被选择的id会被存放在这个数组中
      //props 配置项
      selectedKeys: [],
      cateList: [],
      cateProps: {
        // 选择的值
        value: 'cat_id',
        // 看到的值
        label: 'cat_name',
        expandTrigger: 'hover',
        children: 'children'
      },

      tableData: []
    }
  },
  created () {
    this.getCateList()
  },
  computed: {
    // 数组长度为3，可以点击
    isdisabled () {
      return this.selectedKeys.length !== 3
    },

    //获取到cuteid
    cuteId () {
      if (this.selectedKeys.length === 3) {
        return this.selectedKeys[2]
      } else {
        return null
      }
    }
  },
  methods: {
    async getCateList () {
      const res = await goodsortAPI()
      console.log(res, 'goodsortAPI');
      this.cateList = res.data
    },
    // 选择框值发生变化，会触发函数
    async handleChange (selectedKeys) {
      if (this.activeName == 'many') {
        this.attrbtn()
      } else {

      }

    },
    //点击tab的处理函数
    async handleTabClick () {
      console.log(this.activeName, 'activeName');
      //
      this.attrbtn()

    },

    // 点击添加属性按钮
    async attrbtn () {
      //
      if (this.selectedKeys.length != 3) {
        this.selectedKeys = []
        return
      }
      //当值发生变化时，发送请求
      //渲染
      const res = await choicequeryAPI(this.cuteId, this.activeName)
      console.log(res, 'resres');
      this.tableData = res.data
      res.data.forEach(item => { item.attr_vals = item.attr_vals.split(' ') })
      console.log(res.data, '898989');
    },

    //添加参数和添加参数共用一个对话框
    addAttr (type) {
      this.dialogVisible = true
      if (type == 'query') {
        this.titleTop = '动态参数'
      } else {
        this.titleTop = '静态属性'
      }
    },

    //监听对话框的关闭事件
    addDialogclose () {
      this.dialogVisible = false
      this.$refs.addFormRef.resetFields();
    },

    //点击确认
    addParams () {
      //发送请求前，需要进行兜底校验
      this.$refs.addFormRef.validate(async value => {
        if (value) {
          //可以发送请求
          const res = await addqueryAPI(this.cuteId, this.activeName, this.addForm.attr_name)
          console.log(res, '======');
          this.$message.success('添加成功！')
          this.addDialogclose()
          this.attrbtn()
        } else {
          this.$message.error('请填写属性！')
          this.addDialogclose()
        }
      })
    },
  }
}
</script>
<style lang='less'  scoped>
.el-card {
  margin-top: 20px;

  .el-row {
    margin: 20px 0;
  }
}
</style>
